<template>
  <!-- 添加或修改出口运抵申报管理对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="1100px"
    append-to-body
    :close-on-click-modal="false"
    @close="close"
    class="self-dialog"
  >
    <el-tabs v-model="activeName">
      <el-tab-pane label="基本信息" name="first">
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="108px"
          label-position="right"
          class="flex-form"
          size="mini"
        >
          <el-form-item label="进出口类型" prop="iEFlag">
            <el-select v-model="form.iEFlag" placeholder="请选择进出口类型" disabled filterable>
              <el-option v-for="(item, index) in dict.type.sys_i_e_flag" :key="index"
                :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="关区代码" prop="customsCode">
            <el-select
              v-model="form.customsCode"
              placeholder="请输入进出境海关代码"
              clearable
              filterable
            >
              <el-option
                v-for="(item, index) in customsList"
                :key="index"
                :label="item.value + '-' + item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="批次号" prop="manifestId">
            <el-input
              v-model.trim="form.manifestId"
              placeholder="请输入批次号"
            />
          </el-form-item>
          <el-form-item label="运输模式" prop="transportMode">
            <el-select v-model="form.transportMode" placeholder="请选择运输模式" filterable disabled>
              <el-option v-for="(item, index) in dict.type.bt_transport_mode" :key="index"
                :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运抵时间" prop="arrivarTime">
            <el-date-picker
              clearable
              size="small"
              v-model.trim="form.arrivarTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择运抵时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="卸载地代码" prop="unloadCode">
            <el-input
              v-model.trim="form.unloadCode"
              placeholder="请输入卸载地代码"
            />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model.trim="form.remark"
              type="textarea"
              placeholder="请输入内容"
            />
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="提运单信息" name="second">
        <!-- 提运单list -->
        <bill-list @sendBillList="sendBillList" :arrivarWaybills="arrivarWaybills"></bill-list>
      </el-tab-pane>
      <el-tab-pane label="集装箱信息" name="third">
        <container-list @sendContainerList="sendContainerList" :arrivarContainers="arrivarContainers"></container-list>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getArrivar, addArrivar, updateArrivar } from "@/api/swdecl/swArriveHead";
import billList from "./billList.vue";
import ContainerList from "./containerList.vue";
export default {
  components: { billList, ContainerList },
  name: "Arrivar-addEdit",
  props: {
    dialogType: {
      type: String,
      default: "add",
    },
    rowData: {
      type: Object,
      default: () => {},
    },
    dict: {},
    customsList: {
      type: Array,
      default: () => [],
    }
  },
  data() {
    return {
      // 弹出层
      dialogVisible: false,
      // 标题
      title: "新增",
      activeName: "first",
      // 表单参数
      form: {
        id: null,
        manifestId: null,
        transportMode: "4",
        iEFlag: 'E',
        arrivarTime: null,
        customsCode: null,
        unloadCode: null,
        remark: null,
        reason: null,
        contact: null,
      },
      // 表单校验
      rules: {
        manifestId: [
          { required: true, message: "批次号不能为空", trigger: "blur" },
        ],
        transportMode: [
          { required: true, message: "运输模式不能为空", trigger: "blur" },
        ],
        iEFlag: [
          { required: true, message: "批次号不能为空", trigger: "blur" },
        ],
        arrivarTime: [
          { required: true, message: "运抵时间不能为空", trigger: "blur" },
        ],
        unloadCode: [
          { required: true, message: "卸载地代码不能为空", trigger: "blur" },
        ],
        customsCode: [
          { required: true, message: "关区代码不能为空", trigger: "blur" },
        ],
      },
      // 提运单list
      arrivarWaybills: [],
      // 集装箱list
      arrivarContainers: [],
    };
  },
  methods: {
    open() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        if (this.dialogType == "add") {
          this.title = "新增";
        } else if (this.dialogType == "edit") {
          this.title = "编辑";
          this.loadDetail();
        } else if (this.dialogType == "detail") {
          this.title = "详情";
          this.loadDetail();
        }
      });
    },
    close() {
      this.$refs["form"].resetFields();
      this.arrivarWaybills = [];
      this.arrivarContainers = [];
      this.dialogVisible = false;
    },
    // 获取详情
    loadDetail() {
      const id = this.rowData.id;
      getArrivar(id).then((res) => {
        if (res.code == 200) {
          for(let key in this.form) {
            this.form[key] = res.data[key];
          }
          this.arrivarWaybills = res.data.billsList;
          this.arrivarContainers = res.data.contaList;
        }
      });
    },
    // 提运单list
    sendBillList(val) {
      this.arrivarWaybills = val;
    },
    // 集装箱list
    sendContainerList(val) {
      this.arrivarContainers = val;
    },
    // 提交表单
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let params = {
            ...this.form,
            billsList: this.arrivarWaybills,
            contaList: this.arrivarContainers,
          };
          if (this.dialogType == "add") {
            addArrivar(params).then((res) => {
              if (res.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.$emit("handleQuery");
                this.close();
              }
            });
          } else {
            params.id = this.rowData.id;
            updateArrivar(params).then((res) => {
              if (res.code === 200) {
                this.$modal.msgSuccess("修改成功");
                this.$emit("handleQuery");
                this.close();
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-form-item {
  width: 33%;
}
::v-deep .el-form-item__label {
  line-height: 16px;
  display: flex;
  align-items: center;
  height: 32px;
  justify-content: flex-end;
}
</style>
